package com.easy.web.controller.common;

import com.easy.common.core.R;
import com.easy.common.core.domain.model.LoginBody;
import com.easy.framework.UserVerify;
import com.easy.user.service.UserLoginService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

import static com.easy.common.utils.RedisConstants.LOGIN_USER_KEY;

/**
 * 用户 登录 验证
 *
 * @author RuoYu
 * @date 2022/11/17
 */

@RestController
@RequestMapping
@Api(tags = "登录相关接口")
public class CommonController {

    private static final Logger log = LoggerFactory.getLogger(CommonController.class);

    /*登录*/
    @Autowired
    private UserLoginService userLoginService;

    /*登录校验*/
    @Autowired
    private UserVerify userVerify;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    /**
     * 登录方法
     *
     * @param loginBody 登录身体
     * @param request   请求
     * @return {@link R}
     */
    @PostMapping("login/account")
    @ApiOperation(value = "账户登录方法")
    @ApiImplicitParam(name = "loginBody", value = "登录信息", dataTypeClass = LoginBody.class, required = true)
    public R loginAccount(@RequestBody LoginBody loginBody, HttpServletRequest request) {
        long id = Thread.currentThread().getId();
        System.out.println(id + "登录方法==================");
        if (request.getHeader("authorization") != null) {
            return R.success("请勿重复登陆");
        }

        String msg = userVerify.loadUserByAccount(loginBody.getAccount());
        //1、账户是否能登录
        if (msg.equals("0")) {
            String token = userLoginService.accountLogin(loginBody, loginBody.getPassword());
            //1.1 登录成功，将员工id存入Session并返回登录成功结果
            if (token != null && token.length() > 0) {
                log.info("=================================1");
                return R.success(token);
            }
            return R.error();
        }

        //2、不能登录错误码
        log.error("CA001:{}", msg);
        return R.error(msg);
    }

    /**
     * 手机快捷登录
     *
     * @param loginBody 登录身体
     * @param request   请求
     * @return {@link R}
     */
    @PostMapping("login/phone")
    @ApiOperation(value = "手机快捷登录")
    @ApiImplicitParam(name = "loginBody", value = "登录信息", dataTypeClass = LoginBody.class, required = true)
    public R loginPhone(@RequestBody LoginBody loginBody, HttpServletRequest request) {

        return R.error();
    }


    /**
     * 电子邮件快捷登录
     *
     * @param loginBody 登录身体
     * @param request   请求
     * @return {@link R}
     */
    @PostMapping("login/email")
    @ApiOperation(value = "电子邮件快捷登录")
    @ApiImplicitParam(name = "loginBody", value = "登录信息", dataTypeClass = LoginBody.class, required = true)
    public R loginEmail(@RequestBody LoginBody loginBody, HttpServletRequest request) {
        if (request.getHeader("authorization") != null) {
            return R.success("请勿重复登陆");
        }

        String msg = userVerify.quickByAccount(loginBody.getEmail());
        // 1.账户是否能登录
        if (msg.equals("0") || msg.equals("2")) {
            // 2.进行登录
            String r = userLoginService.emailQuickLogin(loginBody.getEmail(), loginBody.getCode());

            // 2.1 登录成功，将员工id存入Session并返回登录成功结果
            if (r.equals("successful")) {
                return R.success();
            }

            // 2.2登录失败
            return R.error(r);
        }

        // 3.不能登录错误码
        log.error("CA001:{}", msg);
        return R.error(msg);
    }


    /**
     * 注销方法
     *
     * @param request 请求
     * @return {@link R}<{@link String}>
     */
    @GetMapping("/logout")
    @ApiOperation(value = "退出登录方法")
    public R<String> logout(HttpServletRequest request) {
        // 1.获取请求头中的token
        String token = request.getHeader("authorization");

        String key = LOGIN_USER_KEY + token;
        // 2.基于TOKEN删除redis中的用户
        stringRedisTemplate.delete(key);

        return R.success("退出成功");
    }
}
